#include "qstringeditdialog.h"
#include "ui_qstringeditdialog.h"

#include "../sharedLib/qabstractproperty.h"
#include "../sharedLib/qprojectcore.h"
#include "../sharedLib/qlanguagemanager.h"

#include <QLineEdit>
#include <QPainter>
#include <QStyleOptionViewItemV3>
#include <QStyle>
#include <QApplication>
#include <QTreeWidgetItem>
#include <QHeaderView>

using namespace CORELIB;

QWidget *QLanguageDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/*option*/, const QModelIndex &index) const
{
    QWidget *ret=NULL;
    if(index.column()==1)
    {
        ret=new QLineEdit(parent);
    }
    if(ret!=NULL)
    {
        ret->setProperty("no-ManhattanStyle",true);
    }
    return ret;
}

void QLanguageDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
{
    editor->setGeometry(option.rect.adjusted(0,0,0,-1));
}

QSize QLanguageDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QSize size=QItemDelegate::sizeHint(option,index);
    size.setHeight(21);
    return size;
}

void QLanguageDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItemV3 opt=option;

    QColor c=QColor(255,255,191);

    if(opt.features & QStyleOptionViewItemV2::Alternate)
    {
        c=c.lighter(112);
    }
    painter->fillRect(option.rect,c);
    opt.state &=~ QStyle::State_HasFocus;
    QItemDelegate::paint(painter,opt,index);
    opt.palette.setCurrentColorGroup(QPalette::Active);
    QColor color=static_cast<QRgb>(qApp->style()->styleHint(QStyle::SH_Table_GridLineColor,&opt));
    painter->setPen(color);

    int right=option.direction==Qt::LeftToRight?option.rect.right():option.rect.left();
    painter->drawLine(right,option.rect.y(),right,option.rect.bottom());

    painter->drawLine(option.rect.left(),option.rect.bottom(),option.rect.right(),option.rect.bottom());
}

QStringEditDialog::QStringEditDialog(SHAREDLIB::QAbstractProperty *property, QWidget *parent):
    QDialog(parent),
    m_property(property),
    ui(new Ui::QStringEditDialog)
{
    this->setStyle(QApplication::style());
    setProperty("panelwidget",true);
    ui->setupUi(this);

    ui->translateTree->setItemDelegate(new QLanguageDelegate);
    ui->translateTree->setMinimumHeight(0);
    ui->text->setVisible(false);

    ui->translateTree->setAlternatingRowColors(true);
    ui->translateTree->setEditTriggers(QAbstractItemView::EditKeyPressed);
    ui->translateTree->setRootIsDecorated(false);
    ui->translateTree->header()->setMovable(false);

    SHAREDLIB::QLanguageManager *manager=SHAREDLIB::QProjectCore::getCore()->languageManager();

    QObject *p=m_property->object();

    while(p!=NULL)
    {
        if(p->property("host").toInt()!=0)
        {
            m_objectID=p->objectName()+"."+m_objectID;
        }
        p=p->parent();
    }

    m_objectID+=m_property->attribute("property_name").toString();

    ui->widgetID->setText(m_objectID);

    SHAREDLIB::tagLanguageItem  *text=manager->getTextItem(m_objectID);

    int count=manager->languageCount();
    for(int i=0;i<count;i++)
    {
        QObject *obj=manager->getLanguage(i);
        QTreeWidgetItem* item=new QTreeWidgetItem(ui->translateTree);
        item->setText(0,obj->property("languageName").toString());
        if(text!=NULL && text->m_translate.keys().contains(obj))
        {
            item->setText(1,text->m_translate.value(obj));
            item->setToolTip(1,text->m_translate.value(obj));
        }
        else
        {
            item->setText(1,m_property->getValue().toString());
            item->setToolTip(1,m_property->getValue().toString());
        }
        item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable);
        m_items.insert(item,obj);
    }
    ui->text->setText(m_property->getValue().toString());

    connect(ui->translateTree,SIGNAL(clicked(QModelIndex)),ui->translateTree,SLOT(edit(QModelIndex)));
    ui->enabled->setChecked(m_property->attribute("tr").toBool());
    on_enabled_clicked();
}

QStringEditDialog::~QStringEditDialog()
{
    delete ui;
}

void QStringEditDialog::on_enabled_clicked()
{
    bool tran=ui->enabled->isChecked();
    ui->translateTree->setVisible(tran);
    ui->text->setVisible(!tran);
    resize(this->width(),tran?400:100);
    setProperty("panelwidget",tran);
}

void QStringEditDialog::on_okBtn_clicked()
{
    if(ui->enabled->isChecked())
    {
        SHAREDLIB::tagLanguageItem text;
        text.m_id=m_objectID;

        QMapIterator<QTreeWidgetItem*,QObject*>  it(m_items);
        while(it.hasNext())
        {
            it.next();
            text.m_translate.insert(it.value(),it.key()->text(1));
        }
        SHAREDLIB::QLanguageManager *manager=SHAREDLIB::QProjectCore::getCore()->languageManager();
        manager->addTextItem(&text);

        m_property->setSignalValue(manager->getCurrentText(m_objectID));
        m_property->setAttribute("tr",true);
    }
    else
    {
        SHAREDLIB::QProjectCore::getCore()->languageManager()->removeTextItem(m_objectID);
        m_property->setSignalValue(ui->text->text());
        m_property->setAttribute("tr",false);
    }
    close();
}

void QStringEditDialog::on_cancelBtn_clicked()
{
    close();
}
